<?php declare(strict_types=1);


namespace app\model;


class AuthRule extends Base {

    protected $name = 'auth_rule';

    // 设置字段信息
    protected $schema = [
        'pid'  => 'int',
        'rule_id'   =>'int',
        'route_path'=>'string',
        'route_name'=>'string',
        'route_redirect'=>'string',
        'title'=>'string',
        'icon'=>'string',
        'enabled'=>'tinyint',
        'post_url'=>'string',
        'is_menu'=>'tinyint',
        'create_at' => 'datetime',
        'update_at' => 'datetime',
        'delete_at' => 'datetime',
    ];

    //
    public function del(int $ruleId) : bool {
        $menu =  $this->where('rule_id','=',$ruleId)->find();
        if($menu){
            $result = $menu->delete();
            return $result >=1 ? true : false;
        }
        return false;
    }

    public function childrenCount(int $ruleId) : int{
        return $this->where('pid','=',$ruleId)->count();
    }


    public function updateMenu(array $menuData): bool {
        $result = self::where('rule_id','=',$menuData['rule_id'])->save($menuData);
        return $result >=1 ? true : false;
    }

    public function createMenu(array $menuData): bool {
        $result = self::save($menuData);
        return $result >=1 ? true : false;
    }

}

 
